package springtest.webRsa;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

/**
 * @author chenzhiqiang
 * @date 2018-05-11 18:05:06
 */

public class TestIo {
    String publicKeys = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCYHcbM/GasXVKtYxnKDpRF94TxQdKNxE/iGUQ2PFhsCgcASIWDhMNjQjKxtEldzfDXQahc0UZ00yOvUFyatNIYQJSO62DSQIMf/D4t0j1MT+73Q88FFM5nSIDeKp3PkT67O8Cn2StrJS8PyazDpPVtk5eZw3iDcXSbK4Y5+ATIowIDAQAB";
    String privateKeys = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJgdxsz8ZqxdUq1jGcoOlEX3hPFB0o3ET+IZRDY8WGwKBwBIhYOEw2NCMrG0SV3N8NdBqFzRRnTTI69QXJq00hhAlI7rYNJAgx/8Pi3SPUxP7vdDzwUUzmdIgN4qnc+RPrs7wKfZK2slLw/JrMOk9W2Tl5nDeINxdJsrhjn4BMijAgMBAAECgYB4+g12TzH02Q9817jV6nxfnLe9C6VBnCQP5BpMq/eCjQL+Za7RZw/ha+9XRtOmZ7en6g7VKpbHKHhT4CyV2ULQToi8uJE5EgohuoeIAhqiRHnA1uL9VB2qBkG42PIMkfhhvTwtQy7LUmXC3DQDGPieOeR4VPg0w7k6loJoEHo2OQJBANB060rlLD/3W8geEhtY06gA9+i0Eji0Yw3vpS4/ykUA9sj0lWCYMG+/V2A/M7RRsJFpI01yfNEECNFBfMAshfcCQQC6z1Uc2eKWBWoSVkBqgh5f2yZittklkzNvXSOT25Rb+nquqSXcRDzb7be0aOoUIe/uEx+XaknloVF51zJdzje1AkBS0TCtt7DF1QtEzRWagcbNJ11WuTvIBXqZDc2NO1Cr638gRXOXRb9wlg8rsNBk4lOI4cFhwotuKvEp2Z+rZcTvAkEAlj3RkhaaaVWJbZ34yLBFhYpXYOT114Z/w30A9BWRSDg3GunnDTPEToztRidquy5gLPUsXzaYoBoR0RAF7fPAUQJAXmwU7DPSeJ4l0QWXJlNS1A7FsNfZeQE3baNQzzr+5NLGrlhGr76g40v9v7uBquFGLf+a5vY0jLghgu3SoLexYA==";

    public static void main(String[] args) throws Exception {
        String a = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCRt8nUV7hg8ahIrXCgnsNMxZ+jJC1tbyRdce1JSWHdRFc85cmiN0SLNqOi9SPEqBSfeD4EiqyIi0kSOSqYQlw6k6f3vKFAnzJDtf4k5TgMT2XJvHq4ATwu1CWrhk3r/KwnFbJqK7fgjjU4ZzWthTA+gNiv0kDedWTn/FfbrCdX4wIDAQAB";
        String b = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJG3ydRXuGDxqEitcKCew0zFn6MkLW1vJF1x7UlJYd1EVzzlyaI3RIs2o6L1I8SoFJ94PgSKrIiLSRI5KphCXDqTp/e8oUCfMkO1/iTlOAxPZcm8ergBPC7UJauGTev8rCcVsmort+CONThnNa2FMD6A2K/SQN51ZOf8V9usJ1fjAgMBAAECgYA9bDQ2PreonZSq9fj4q5N1RARTd0dyzpuMjB5Sew02gL0pHU9OAm/xIpn9ccme9p6izcOOlIm/kL6/YLt0rNxE7JdIykFoB1T92SxS8K0LohauWLFyKgvMCb0oBcmSvtGpGP6G5zYRwE3dnu4Jtvy2qKSm+rikXrwiTCujc7BvGQJBANuT3iZcEzkup+bRZg98s2aDQ4+t9YReLGmwxBO5LFkaCSzpjZvL+Br69EbjTpcp47l7yM39KPlRHbRuOdNmeKUCQQCp44v5M9rQTlhVv4O/qtSo2OHnvoumiogket/Nh+PvHIBRhfuZhbi0Bpp8rfWlxCxWs5Rxy1bkqz1rkDDhGp/nAkAbvdrbTda/6r0b1GjonACuevcWOAxE5QEHeWX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c2NO1Cr638gRXOXRb9wlg8rsNBk4lOI4cFhwotuKvEp2Z+rZcTvAkEAlj3RkhaaaVWJbZ34yLBFhYpXYOT114Z/w30A9BWRSDg3GunnDTPEToztRidquy5gLPUsXzaYoBoR0RAF7fPAUQJAXmwU7DPSeJ4l0QWXJlNS1A7FsNfZeQE3baNQzzr+5NLGrlhGr76g40v9v7uBquFGLf+a5vY0jLghgu3SoLexYA==";
        String data = "test";
        PrivateKey privateKey = RSAUtils.getPrivateKey(b);
        PublicKey publicKey = RSAUtils.getPublicKey(a);
        // 公钥加密
        String encryptByPublicKey = RSAUtils.encryptByPublicKey(data, (RSAPublicKey) publicKey);
        System.out.println(encryptByPublicKey);
        // 私钥解密
        String string = RSAUtils.decryptByPrivateKey(encryptByPublicKey, (RSAPrivateKey) privateKey);
        System.out.println(string);

    }

    /**
     * 使用RSA私钥加密数据
     * 
     * @param pubKeyInByte
     *            打包的byte[]形式私钥
     * @param data
     *            要加密的数据
     * @return 加密数据
     */
    public static byte[] encryptByRSA1(byte[] privKeyInByte, byte[] data) {
        try {
            PKCS8EncodedKeySpec priv_spec = new PKCS8EncodedKeySpec(privKeyInByte);
            KeyFactory mykeyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privKey = mykeyFactory.generatePrivate(priv_spec);
            Cipher cipher = Cipher.getInstance(mykeyFactory.getAlgorithm());
            cipher.init(Cipher.ENCRYPT_MODE, privKey);
            return cipher.doFinal(data);
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * 用RSA公钥解密
     * 
     * @param privKeyInByte
     *            公钥打包成byte[]形式
     * @param data
     *            要解密的数据
     * @return 解密数据
     */
    public static byte[] decryptByRSA1(byte[] pubKeyInByte, byte[] data) {
        try {
            KeyFactory mykeyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec pub_spec = new X509EncodedKeySpec(pubKeyInByte);
            PublicKey pubKey = mykeyFactory.generatePublic(pub_spec);
            Cipher cipher = Cipher.getInstance(mykeyFactory.getAlgorithm());
            cipher.init(Cipher.DECRYPT_MODE, pubKey);
            return cipher.doFinal(data);
        } catch (Exception e) {
            return null;
        }
    }
}
